package com.wicket.okrpbff.biz.service.nb;

import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.integration.dto.FrameworkPrivacyDto;
import com.wicket.okrframework.integration.dto.FrameworkSceneRoleDto;
import com.wicket.okrpbff.biz.service.dto.req.AddCollectionRemoveExistingDataReqDto;
import com.wicket.okrpbff.biz.service.dto.req.BatchClearUserInfoFromRedisReqDto;
import com.wicket.okrpbff.biz.service.dto.req.CheckCollectionRemoveExistingDataReqDto;
import com.wicket.okrpbff.biz.service.dto.res.AddCollectionRemoveExistingDataRespDto;
import com.wicket.okrpbff.biz.service.dto.res.BatchClearUserInfoFromRedisRespDto;
import com.wicket.okrpbff.biz.service.dto.res.CheckCollectionRemoveExistingDataRespDto;
import com.wicket.okrpbff.common.util.RedisUtil;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbPlfAuth {

    @Resource
    private RedisUtil redisClient;
    /**
     * code:collectionRemoveExistingData
     * name:D检查集合去除已存在数据
     * desc:undefined
     * gen by moon at 2022/7/4 下午6:49:24
     **/
    @Trace(operationName = "D检查集合去除已存在数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @BanAuto
    public CheckCollectionRemoveExistingDataRespDto checkCollectionRemoveExistingData(CheckCollectionRemoveExistingDataReqDto reqDto) {
        CheckCollectionRemoveExistingDataRespDto retData = new CheckCollectionRemoveExistingDataRespDto();
        List<FrameworkPrivacyDto> filteredPrivacyList = new ArrayList<>(reqDto.getFrameworkPrivacyList());
        for (FrameworkPrivacyDto privacyOne : reqDto.getFrameworkPrivacyList()) {
            for (String filterdId : reqDto.getPlfAuthList()) {
                if (privacyOne.getPrivacyId().equals(filterdId)) {
                    filteredPrivacyList.remove(privacyOne);
                }
            }
        }
        retData.setFrameworkPrivacyList(filteredPrivacyList);
        return retData;
    }

    /**
     * code:collectionRemoveExistingData
     * name:D授权角色/权限去除已存在
     * desc:undefined
     * gen by moon at 2022/7/4 下午6:49:39
     **/
    @Trace(operationName = "D授权角色/权限去除已存在")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @BanAuto
    public AddCollectionRemoveExistingDataRespDto addCollectionRemoveExistingData(AddCollectionRemoveExistingDataReqDto reqDto) {
        AddCollectionRemoveExistingDataRespDto retData = new AddCollectionRemoveExistingDataRespDto();
        List<FrameworkSceneRoleDto> filteredRoleList = new ArrayList<>();
        for (FrameworkSceneRoleDto role : reqDto.getRoleList()) {
            if (!reqDto.getRoleIdList().contains(role.getRoleId())) {
                filteredRoleList.add(role);
            }
        }
        retData.setRoleList(filteredRoleList);
        return retData;
    }

    /**
     * code:batchClearUserInfoFromRedis
     * name:Redis批量清空用户信息
     * desc:undefined
     * gen by moon at 2022/7/4 下午6:56:31
     **/
    @Trace(operationName = "Redis批量清空用户信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @BanAuto
    public BatchClearUserInfoFromRedisRespDto batchClearUserInfoFromRedis(BatchClearUserInfoFromRedisReqDto reqDto) {
        if (reqDto.getBeginMemberList() == null || reqDto.getBeginMemberList().isEmpty()) {
            return new BatchClearUserInfoFromRedisRespDto();
        }
        for (String member : reqDto.getBeginMemberList()) {
            redisClient.delete("userbase-" + member);
            redisClient.delete("inductionInfo-" + member);
        }
        return new BatchClearUserInfoFromRedisRespDto();
    }
    //手工接入方法
}
